//
//  ZPStatusCell.swift
//  ZPWeibo
//
//  Created by zhangpeng on 16/4/19.
//  Copyright © 2016年 zhangpeng. All rights reserved.
//

import UIKit
import SnapKit
class ZPStatusCell: UITableViewCell {

    // 记录我们的底部toolBar 的约束
    var toolBarTopConstraint: Constraint?
    
    var status:ZPStatus?{
    
        didSet{
         originalView.status = status
            
            // 在重新给toolbar设置top约束的时候需要干掉以前的约束
            // uninstall() 卸载约束  这里约束只有自己，不能有其他控件
            self.toolBarTopConstraint?.uninstall()
            
            /*
            分析是否有转发微博
            - 有 -- status?.retweeted_status != nil
            - 没有 -- status?.retweeted_status == nil
            */
            // 没有转发微博
            
            if status?.retweeted_status == nil{
            
                 // 隐藏转发微博(由于cell复用的问题)
                retweetView.hidden = true
                
                //重新设置约束  toolbar 的顶部 ==  原创微博的底部
                toolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }else{
            
                //有
                retweetView.hidden = false
                
                //设置数据
                retweetView.status = status
                
                toolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }
              // 给底部toolbar 设置数据
            toolBarView.status = status
        }
    }
    

    override init(style:UITableViewCellStyle, reuseIdentifier:String?){
    
        super.init(style:style, reuseIdentifier:reuseIdentifier)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
    
         self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 01- 添加控件
        contentView.addSubview(originalView)
        
        contentView.addSubview(retweetView)
        
        contentView.addSubview(toolBarView)
        
        
        
          // 02- 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            //make.height.equalTo(50)
        }
        
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
           
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(37)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
    
    
    // MARK: - 懒加载控件
    //1.原创微博
    private lazy var originalView : ZPStatusOriginalView = ZPStatusOriginalView()
    
    //2.底部转发的评论赞
    private lazy var toolBarView: ZPStatusToolBarView = ZPStatusToolBarView()
    //3.转发微博
    private lazy var retweetView: ZPStatusRetweetView = ZPStatusRetweetView()
    
}

//extension ZPStatusCell{
//
//      // 分析报错
//    func demo(){
//    
//    }
//}

